package com.financing.task.invest;
import com.financing.enums.notify.AgentNotify;
import com.financing.enums.notify.AssetsNotify;
import com.financing.model.invest.CouponLog;
import com.financing.model.user.User;
import com.financing.service.invest.CouponLogService;
import com.financing.service.invest.CouponService;
import com.financing.service.system.AssetsLogService;
import com.financing.util.Str;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 优惠券过期提醒任务
 * @author 无处不在的二叔
 */
public class CouponLogExpireTask implements Runnable{
	private AssetsLogService assetsLog = new AssetsLogService();	
	private CouponService coupon = new CouponService();	
	private CouponLogService couponLog = new CouponLogService();	
    
	@Override 
	@Before(Tx.class)
	public void run() {
		
		// logger.error("优惠券记录过期提醒任务开始！");
		coupon.updateExpired();
		
		for(CouponLog cl : couponLog.findCloseToExpire()){
			// 获取用户
			User u = User.me.findById(cl.getUserId());

			// 获取优惠券名称 
			String couponName= cl.getName();
			
			// 发送优惠券即将过期资产提醒
			AssetsNotify assets = AssetsNotify.COUPON_CLOSE_TO_EXPIRE;
			AgentNotify agentNotify = AgentNotify.COUPON_CLOSE_TO_EXPIRE;
			assetsLog.bulid(u, Str.format(assets.getName(), couponName), Str.format(assets.getContents(), couponName), Str.format(agentNotify.getContents(), u.getMobile()+"（"+u.getRealName()+"）", couponName));
			
			// 发送优惠券即将过期短信通知
			// Sms.send(u.getMobile(), Str.format(SmsNotify.CLOSE_TO_EXPIRE, couponName));
			
			// 标记为已提醒
			cl.setIsNotify(1).update();
		}
		
		// logger.error("优惠券已过期提醒任务开始！");
		
		for(CouponLog cl : couponLog.findExpire()){
			// 获取用户
			User u = User.me.findById(cl.getUserId());
			
			// 获取优惠券名称
			String couponName= cl.getName();

			// 发送优惠券已过期资产提醒
			AssetsNotify assets = AssetsNotify.COUPON_EXPIRE;
			AgentNotify agentNotify = AgentNotify.COUPON_EXPIRE;
			assetsLog.bulid(u, Str.format(assets.getName(), couponName), Str.format(assets.getContents(), couponName), Str.format(agentNotify.getContents(), u.getMobile()+"（"+u.getRealName()+"）", couponName));

			cl.setStatus("expire").update();
			
			// 发送优惠券即将过期短信通知
			//Sms.send(u.getMobile(), Str.format(SmsNotify.EXPIRE, couponName));
		}
	}
}
